package com.boot.json.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.databind.ser.std.StringSerializer;

import java.io.IOException;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 序列化 将空字符串转化为null (可以直接使用 default-property-inclusion: non_empty )
 * @date 2023/4/25 17:01
 **/

@Deprecated
public class StringToNullSerializer extends StdSerializer<String> {

    public static final StringToNullSerializer INSTANCE = new StringToNullSerializer();
    private static final StringSerializer STRING_SERIALIZER_INSTANCE = new StringSerializer();

    protected StringToNullSerializer() {
        super(String.class);
    }

    @Override
    public void serialize(String v, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (v == null || v.isEmpty()) {
            STRING_SERIALIZER_INSTANCE.serialize(null, gen, provider);
        } else {
            STRING_SERIALIZER_INSTANCE.serialize(v, gen, provider);
        }
    }
}
